import {
  deletePageData,
  getPageListData,
  createPageData,
  editPageData
} from '@/service/main/system/system'
import { Module } from 'vuex'
import { IRootState } from '../../types'
import { ISystemState } from './types'
const systemModule: Module<ISystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      userList: [],
      userCount: 0,
      roleList: [],
      roleCount: 0,
      goodsList: [],
      goodsCount: 0,
      menuList: [],
      menuCount: 0,
      departmentCount: 0,
      departmentList: [],
      dashboardCount: 0,
      dashboardList: [],
      categoryCount: 0,
      categoryList: [],
      storyCount: 0,
      storyList: []
    }
  },
  mutations: {
    changeusersList(state, userList: any[]) {
      state.userList = userList
    },
    changeusersCount(state, userCount: number) {
      state.userCount = userCount
    },
    changeroleList(state, roleList: any[]) {
      state.roleList = roleList
    },
    changeroleCount(state, roleCount: number) {
      state.roleCount = roleCount
    },
    changegoodsList(state, goodsList: any[]) {
      state.goodsList = goodsList
    },
    changegoodsCount(state, goodsCount: number) {
      state.goodsCount = goodsCount
    },
    changemenuList(state, menuList: any[]) {
      state.menuList = menuList
    },
    changemenuCount(state, menuCount: number) {
      state.menuCount = menuCount
    },
    changedepartmentList(state, List: any[]) {
      state.departmentList = List
    },
    changedepartmentCount(state, Count: number) {
      state.departmentCount = Count
    },
    changedashboardList(state, List: any[]) {
      state.departmentList = List
    },
    changedashboardCount(state, Count: number) {
      state.departmentCount = Count
    },
    changecategoryList(state, List: any[]) {
      state.categoryList = List
    },
    changecategoryCount(state, Count: number) {
      state.categoryCount = Count
    },
    changestoryList(state, List: any[]) {
      state.storyList = List
    },
    changestoryCount(state, Count: number) {
      state.storyCount = Count
    }
  },
  getters: {
    PageListData(state) {
      return (pageName: string) => {
        if (pageName === 'users') {
          pageName = pageName.slice(0, 4)
        }
        return (state as any)[`${pageName}List`] ?? []
      }
    },
    PageListCount(state) {
      return (pageName: string) => {
        if (pageName === 'users') {
          pageName = pageName.slice(0, 4)
        }
        return (state as any)[`${pageName}Count`] ?? 0
      }
    }
  },
  actions: {
    // 获取数据
    async getPageListAtion({ commit }, payload: any) {
      // 对传入的pageurl进行处理
      const pageName = payload.pageName
      const pageUrl = `/${pageName}/list`
      if (pageUrl.length === 0) return
      // 1.对页面发送请求
      const pageResult = await getPageListData(pageUrl, payload.queryInfo)
      const { list, totalCount } = pageResult.data ?? []

      // // 2.将数据存储到state中
      commit(`change${pageName}List`, list)
      commit(`change${pageName}Count`, totalCount)
    },
    // 删除数据
    async deletePageDataAction({ dispatch }, payload: any) {
      // payload: 有 pageName id  -> /users/id
      const { pageName, id } = payload
      const pageUrl = `/${pageName}/${id}`
      const isDelete = await deletePageData(pageUrl)
      console.log(isDelete)
      // 请求最新的数据
      dispatch('getPageListAtion', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },
    // 新建数据
    async createPageDataAction({ dispatch }, payload: any) {
      // 新建数据的请求
      const { pageName, newData } = payload
      const pageUrl = `/${pageName}`
      const isCreated = await createPageData(pageUrl, newData)
      console.log(isCreated)
      // 请求最新的数据
      dispatch('getPageListAtion', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },
    // 编辑数据
    async editPageDataAction({ dispatch }, payload: any) {
      // 编辑数据的请求
      const { pageName, editData, id } = payload
      const pageUrl = `/${pageName}/${id}`
      const isCreated = await editPageData(pageUrl, editData)
      console.log(isCreated)
      // 请求最新的数据
      dispatch('getPageListAtion', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    }
  }
}
export default systemModule
